package com.app.TCES.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.app.TCES.common.MyException;
import com.app.TCES.domain.Indicators;
import com.app.TCES.domain.Teacher;
import com.app.TCES.domain.TeacherSet;
import com.app.TCES.mapper.TeacherMapper;




@Service
public class TeacherService {
    @Autowired
	TeacherMapper teacherMapper;
	public void TchCGPW(Teacher teacher) {
		
		teacherMapper.tchCGPW(teacher);
	}
	public int getCouseId(TeacherSet teacherSet) {
		
		return teacherMapper.getCourseId(teacherSet);
	}
	public int getDeptId(TeacherSet teacherSet) {
		
		return teacherMapper.getDeptId(teacherSet);
	}
	public void insertTeacher(int deptId, TeacherSet teacherSet) {
		
		teacherMapper.insertTeacher(deptId,teacherSet);
	}
	public void insertTeacherCourse(int courseId, TeacherSet teacherSet) {
		
		teacherMapper.insertTeacherCourse(courseId,teacherSet);
			
		
	}
	public int selectTeacher(TeacherSet teacherSet) {
		
		return teacherMapper.selectTeacher(teacherSet);
	}
	public void deleteTeacher(int tchId) {
		
		 teacherMapper.deleteTeacher(tchId);
	}
	public void updateTeacherDN(int deptId, int tchId) {
		
		teacherMapper.updateTeacherDN(deptId,tchId);
	}
	public int getDeptId1(String value) {
		
		return teacherMapper.getDeptId1(value);
	}
	public int getCouseId1(String value) {
		
		return teacherMapper.getCourseId1(value);
	}
	public void updateTeacherCN(int courseId, int rid) {
		
		 teacherMapper.updateTeacherCN(courseId,rid);
		
	}
	public List<TeacherSet> getTeacherFuzzyQ(String text, int tmp, Integer limit) {
	
		return teacherMapper.getTeacherFuzzyQ(text,tmp,limit);
	}
	
	public List<TeacherSet> getTeacherFuzzyQ1(String text, String deptName, int tmp, Integer limit) {
		
		return teacherMapper.getTeacherFuzzyQ1(text,deptName,tmp,limit);
	}
	public int getto12(String text) {
		
		return teacherMapper.gettol2(text);
	}
	public int getto13(String text, String deptName) {
		
		return teacherMapper.gettol3(text,deptName);
	}
	
	@SuppressWarnings({ "deprecation", "resource" })
	@Transactional(readOnly = false,rollbackFor = Exception.class)

	    public boolean batchImport(String fileName, MultipartFile file) throws IOException {
		boolean notNull = false;
		List<TeacherSet> teacherList = new ArrayList<>();
		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			throw new MyException("上传文件格式不正确");
		}
		boolean isExcel2003 = true;
		if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
			isExcel2003 = false;
		}
		InputStream is = file.getInputStream();
		Workbook wb = null;
		if (isExcel2003) {
			wb = new HSSFWorkbook(is);
		} else {
			wb = new XSSFWorkbook(is);
		}
		Sheet sheet = wb.getSheetAt(0);
		if(sheet!=null){
			notNull = true;
		}
		TeacherSet teacherSet;
		for (int r = 2; r <= sheet.getLastRowNum(); r++) {//r = 2 表示从第三行开始循环 如果你的第三行开始是数据
			Row row = sheet.getRow(r);//通过sheet表单对象得到 行对象
			if (row == null){
				continue;
			}

			 System.out.println(sheet.getLastRowNum());
			//sheet.getLastRowNum() 的值是 10，所以Excel表中的数据至少是10条；不然报错 NullPointerException

			teacherSet = new TeacherSet();


			
			row.getCell(0).setCellType(CellType.STRING);//得到每一行的 第1个单元格的值
			String x = row.getCell(0).getStringCellValue();//得到每一行第一个单元格的值

	     


			int tchId ;

			if( x== null || x.isEmpty()){//判断是否为空
				throw new MyException("导入失败(第"+(r+1)+"行,工号未填写)");
			}else {
				 tchId = Integer.parseInt(x);
			}


			row.getCell(1).setCellType(CellType.STRING);//得到每一行的 第二个单元格的值
			String tchName = row.getCell(1).getStringCellValue();


			if(tchName==null || tchName.isEmpty()){
				throw new MyException("导入失败(第"+(r+1)+"行,教师名未填写)");
			}
			
			row.getCell(2).setCellType(CellType.STRING);//得到每一行的 第三个单元格的值
			String courseName = row.getCell(2).getStringCellValue();


			if(courseName==null || courseName.isEmpty()){
				throw new MyException("导入失败(第"+(r+1)+"行,课程名未填写)");
			}

			row.getCell(3).setCellType(CellType.STRING);//得到每一行的 第四个单元格的值
			String deptName = row.getCell(3).getStringCellValue();


			if(deptName==null || deptName.isEmpty()){
				throw new MyException("导入失败(第"+(r+1)+"行,院系未填写)");
			}

			//完整的循环一次 就组成了一个对象
			teacherSet.setTchId(tchId);
			teacherSet.setTchName(tchName);
			teacherSet.setCourseName(courseName);
			teacherSet.setDeptName(deptName);
			teacherList.add(teacherSet);
		}
		
		for (TeacherSet teacherResord : teacherList) {
			String tchName = teacherResord.getTchName();
			int cnt = teacherMapper.selectByTchName(tchName);
			int deptId = teacherMapper.getDeptId1(teacherResord.getDeptName());
			int courseId = teacherMapper.getCourseId1(teacherResord.getCourseName());
			if (cnt == 0) {
				
				teacherMapper.addTeacher(teacherResord,deptId);
				teacherMapper.addTeacherZJ(teacherResord,courseId);
				System.out.println(" 插入 "+teacherResord);
			} else {

				System.out.println(" 更新 "+teacherResord);
			}
		}
		return notNull;

	}
	public int getDeptId2(String tchName) {
		
		return teacherMapper.getDeptId2(tchName);
	}
	public List<TeacherSet> selectTeacher1(int deptId, int tchId) {

		return teacherMapper.selectTeacher1(deptId,tchId);
	}
	public int getTchId(String tchName) {
	
		return teacherMapper.getTchId(tchName);
	}
	public Indicators selectIndicators(int eva_teacher_id, int tchId) {
		
		return teacherMapper.selectIndicators(eva_teacher_id,tchId);
	}
	public void TonghangDaFen(int score, int eva_teacher_id, int tchId) {
		teacherMapper.TonghangDaFen(score,eva_teacher_id,tchId);
		
	}

	public int selectScore(int tchId) {
		
		return teacherMapper.selectScore(tchId);
	}
	public int selectThScore(int tchId) {
		
		return teacherMapper.selectThScore(tchId);
	}
	public void TeacherRP(int tchId) {
		
		teacherMapper.TeacherRP(tchId);
	}


	
}
